﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace ConfigurationSectionDesigner
{
    /// <summary>
    /// Represents a list of attributes.
    /// </summary>
    public class AttributeList : LinkedList<Attribute>
    {
        /// <summary>
        /// Converts the list of attributes into its string representation.
        /// </summary>
        /// <returns>A string representation of this list of attributes.</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if( this.Count > 0 )
            {
                // List each attribute separated with a comma and a space
                foreach( Attribute attribute in this )
                    sb.AppendFormat( "{0}, ", attribute.ToString() );

                // Remove the final comma and space
                sb.Remove( sb.Length - 2, 2 );
            }
            return sb.ToString();
        }
    }

    /// <summary>
    /// Represents an attribute.
    /// </summary>
    public class Attribute
    {
        /// <summary>
        /// The name of the attribute. This should be a Fully Qualified Name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The list of parameters for this attribute.
        /// </summary>
        public LinkedList<AttributeParameter> Parameters { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> class
        /// with no parameters.
        /// </summary>
        public Attribute()
        {
            Parameters = new LinkedList<AttributeParameter>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Attribute"/> class
        /// with the given parameters.
        /// </summary>
        /// <param name="parameters"></param>
        public Attribute( params AttributeParameter[] parameters )
        {
            Parameters = new LinkedList<AttributeParameter>( parameters );
        }

        /// <summary>
        /// Converts the attribute into its string representation.
        /// </summary>
        /// <returns>A string representation of this attribute.</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append( "[" );
            sb.Append( Name );
            if( Parameters.Count > 0 )
            {
                sb.Append( "(" );
                foreach( AttributeParameter parameter in Parameters )
                    sb.AppendFormat( "{0}, ", parameter.ToString() );
                sb.Remove( sb.Length - 2, 2 );
                sb.Append( ")" );
            }
            sb.Append( "]" );

            return sb.ToString();
        }
    }

    /// <summary>
    /// Represents a parameter in an attribute.
    /// </summary>
    public class AttributeParameter
    {
        /// <summary>
        /// The name of a name attribute parameter. Set to null if
        /// the parameter is not named.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The value of the parameter.
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// Converts the parameter into its string representation.
        /// </summary>
        /// <returns>A string representation of this parameter.</returns>
        public override string ToString()
        {
            if( !string.IsNullOrEmpty( Name ) )
                return string.Format( "{0} = {1}", Name, Value );
            else
                return Value;
        }
    }
}
